﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Way.EAP.DataAccess.Entity;
using Way.Bus.Model;
using Way.Contract.MVC;

namespace Way.Bus.WMS.Model
{
    /// <summary>
    /// 描述：出库通知明细表
    /// 作者：柳强
    /// 创建日期:2013-02-19
    /// </summary>
    [EntityClassAttribut("Acc_WMS_OutNoticeMaterials", "出库通知明细", IsOnAppendProperty = true)]
    public class StockOutNoticeMaterials : OrderMaterialsCommon
    {
        /// <summary>
        /// 来源单号
        /// </summary>
        [EntityControl("来源单号", true, true, 1)]
        [EntityForeignKey(typeof(StockOutNotice), "ID", "CODE")]
        [EntityField(8)]
        public int PARENTID { get; set; }

        /// <summary>
        /// 单品重量
        /// </summary>
        [EntityControl("单品重量", false, true, 9)]
        [EntityField(10, IsNotNullable = false)]
        public double SINGLEWEIGHT { get; set; }

        /// <summary>
        /// 总重量
        /// </summary>
        [NotSearchAttribut]
        [EntityControl("总重量", false, true, 10)]
        [EntityField(10)]
        public double MWEIGHT { get; set; }

        /// <summary>
        /// 库存数量
        /// </summary>
        [NotSearchAttribut]
        [EntityControl("库存数量", true,true, 13)]
        //[EntityField(10)]
        public double KCNUM { get; set; }
        /// <summary>
        /// 预占用数量
        /// </summary>
        [NotSearchAttribut]
        [EntityControl("预占用数量", true, true, 14)]
        public double YZYNUM { get; set; }

        protected override string GetSearchSQL()//增加总重自动计算
        {
            StockOutOrderMaterials siom = new StockOutOrderMaterials();
            string bm = this.ToString();
            string sql = base.GetSearchSQL();
            string nsql = "select * from (select a.*,isnull(b.FINISHNUM,0) FINISHNUM,a.num-isnull(b.FINISHNUM,0) STAYNUM,a.num*isnull(a.SINGLEWEIGHT,0) MWEIGHT,c.CUSTOMEAN13,CASE WHEN a.batchno is null or a.batchno='' THEN  f.zkcnum   ELSE  isnull(d.kcnum,0)  END as KCNUM ,(e.yznum-e.fcnum) YZYNUM from (" + sql + ") a"
                + " left join (select SourceRowID,sum(num) FINISHNUM from " + siom.ToString() + " group by  SourceRowID)  b on b.SourceRowID=a.id"
            + " left join Acc_Bus_BusinessCommodity c on c.id=a.MATERIALCODE"
            + " left join (select Code,BATCHNO,SUM(NUM) kcnum from Acc_WMS_InfoMaterials group by Code,BATCHNO) d on d.code=a.MATERIALCODE and d.batchno=a.batchno"
            +" left join (select a.MATERIALCODE,SUM(a.num) yznum,isnull(SUM(b.num),0) fcnum from Acc_WMS_OutNoticeMaterials a"
            + " left join (select sourcerowid, MATERIALCODE,SUM(num) num from  Acc_WMS_OutOrderMaterials group by sourcerowid, MATERIALCODE) b"
            +" on a.id=b.sourcerowid and a.MATERIALCODE=b.MATERIALCODE"
            + " group by a.MATERIALCODE) e on a.MATERIALCODE=e.MATERIALCODE"
            + " left join (select Code,SUM(NUM) zkcnum from Acc_WMS_InfoMaterials group by Code) f on f.code=a.MATERIALCODE "
            +") " + bm;
            //string nsql = "select " + bm + ".*,isnull(b.FINISHNUM,0) FINISHNUM," + bm + ".num-isnull(b.FINISHNUM,0) STAYNUM from (" + sql + ") " + bm
            //    + " left join (select SourceRowID,sum(num) FINISHNUM from " + siom.ToString() + " group by  SourceRowID)  b on b.SourceRowID=" + bm + ".id";
            return nsql;
        }

        
        ///// <summary>
        ///// 外系统数据关系集合
        ///// </summary>
        //private HierarchicalEntityView<StockOutNoticeMaterials, StockOutNoticeMaterialsMapping> _MapItems;
        //[HierarchicalEntityControl(visible = false)]
        //public HierarchicalEntityView<StockOutNoticeMaterials, StockOutNoticeMaterialsMapping> MapItems
        //{
        //    get
        //    {
        //        if (_MapItems == null)
        //            _MapItems = new HierarchicalEntityView<StockOutNoticeMaterials, StockOutNoticeMaterialsMapping>(this);
        //        return _MapItems;
        //    }
        //}
        private HierarchicalEntityView<StockOutNoticeMaterials, OutNoticeSequence> _OutSequence;
        ///<summary>
        ///序列码
        ///</summary>
        //[HierarchicalEntityControl(isadd = false, isedit = false, ischeck = false)]
        public HierarchicalEntityView<StockOutNoticeMaterials, OutNoticeSequence> OutSequence
        {
            get
            {
                if (_OutSequence == null)
                {
                    _OutSequence = new HierarchicalEntityView<StockOutNoticeMaterials, OutNoticeSequence>(this);
                }
                return _OutSequence;
            }
        }
    }
    [EntityClassAttribut("WMS_OutNoticeSequence", "序列号", IsOnAppendProperty = true)]
    public class OutNoticeSequence : BusinessBase
    {
        /// <summary>
        /// 主表ID
        /// </summary>
        [EntityForeignKey(typeof(StockOutNoticeMaterials), "PARENTID")]
        [EntityField(20)]
        public int MAINID { get; set; }
        /// <summary>
        /// 行ID
        /// </summary>
        [EntityForeignKey(typeof(StockOutNoticeMaterials), "ID", "MATERIALCODE")]
        [EntityField(20)]
        public int PARENTID { get; set; }
        /// <summary>
        /// 序列码
        /// </summary>
        [EntityControl("序列号", false, true, 2)]
        [EntityField(200)]
        public string SEQUENCECODE { get; set; }
    }
}
